package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.CompanyDaoImpl;
import com.ysd.entity.Company;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class adminCompanyServlet extends BaseServlet {
	/**
	 * localhost:8080/kouhong/adminCompany?reqName=selCompany
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	
	public void selCompany(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		CompanyDaoImpl cDao = new CompanyDaoImpl();
		Company c = cDao.selCompany();
		System.out.println(c);
		Gson gson = new Gson();
		out.print(gson.toJson(c));
		out.flush();
		out.close();
	}

	/**
	 * localhost:8080/kouhong/adminCompany?reqName=getCompany
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	
	public void getCompany(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		CompanyDaoImpl cDao = new CompanyDaoImpl();
		int id =0;
		if(request.getParameter("id")!=null && !request.getParameter("id").equals("")) {
			id = Integer.parseInt(request.getParameter("id"));
		}
		Company c = cDao.getCompanyById(id);
		Gson gson = new Gson();
		out.print(gson.toJson(c));

		out.flush();
		out.close();
	}
	
	/**
	 * localhost:8080/kouhong/adminCompany?reqName=updateCompany
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	
	public void updateCompany(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		CompanyDaoImpl cDao = new CompanyDaoImpl();
		int id =0;
		if(request.getParameter("id")!=null&&!request.getParameter("id").equals("")) {
			 id = Integer.parseInt(request.getParameter("id"));
		}
		String name = request.getParameter("name");
		String website = request.getParameter("website");
		String introduction = request.getParameter("introduction");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		String address = request.getParameter("address");
		String culture = request.getParameter("culture");
		String logo = request.getParameter("logo");
		
		
		Company c = new Company(id, name, website, introduction, phone, email, address, culture, logo);
		int n = cDao.updateCompany(c);

		if (n>0) {
			out.print(Result.toClient(true, "修改成功", ""));
		} else {
			out.print(Result.toClient(false, "修改失败", ""));
		}
		out.flush();
		out.close();
	}


}
